				Assignment no.2

Aim:Writing simple vc++ programs.

  #include<windows.h>
//# indicates directive
//directives are direct instructions to the complier
//macros are also directives
//directives are executed before compilation
//include tells vc++ that the program has identifiers defined in the header file
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdline,int iCmdShow)
//WinMain is lijke main() of c or c++.Also called as program entry point.
//Its the starting point of execution of windows program.
//It is called by the operating system along with arguement.
//Its prototype is defined in winbase.h
//WINAPI refers to calling convention.It is a macro.It is defined in windef.h
//when WINAPI means_stdcall(Standard Calling Convention) the following holds:-

******************************
Arguement -Passing order  Right to left
Arguement -Passing convention By value,
		unless a pointer or
		reference type is
		passed.

Stack-maintenanace responsibility calling function 
			pops its own 
			arguement from the stack.

Name decoration         An underscore(_) is predefined 
convention followed    to the name.The name is followed by the at the sign(@)
by the number of bytes(in decimal) in the arguement lis.Therefore ,the function declared as int func (int a.double b)is decorated as follows:_func@12 Case -translation Conmventio None
*/
//HINSTANCE is the name of user defined type and hInstance is a varible.
//h is prefix means handel which is 32-bit pointer used for reference.its type is normally void*
//Instance refers to the aplication that is running
//h instance contains address of memory where application data and function are stored.
//h prev instance is null in win2000 and above otherwise it contains handel to previous instance of the application.
//PSTR is pointer to string(char*)
//Sz stands for string terminated with zero
***************************
//Sz CmdLine Contains Command line arguments without the exe name(Command with arguments specified using)
//Start -> run option or from the DOS prompt
//iCmdShow contains numbers that indicates whether window is to be shown in maximized,minimized or in restored form.
//its value can be SW_MAXIMIZE,SW_MINIMIZE ot SW_RESTORE defined in winuser.h
{
MessageBox(NULL,"Helloworld","My first program",MB_ICONINFORMATION!MB_OK);
//(handle to owner window,text in message box,message bo title,message box style i.e. icons and buttons)
// is binary or operator return ;
}
****************************
The Messagebox Function
The messagebox function create,displays and operates amessagebox.
The messagebox contion an application -defined message and title,plus any combination of predefine action and push buttons.
Messagebox (Null,"Hellow World","My first Program....."MB_InconINFORMATION,MB_OK);
//(handle to owner window,text in messagebox title,messagebox style ie icon and buttons)
//is a binary or operate
   	VALUE					MEANING
MB_ABORTETRVGNORE			The messagebox contains three push buttons
					Abort Retry and Ignore.


MB_CANCELTRVCONTINUE			window 2000;the messagebox cpntains three
					push buttons cancle,Try Again,continuE

Use this message box type instead of MB_ABORTRETRVINGNORE

MB_HELP					window 95/98,window NT4.0 and later:adds
					a help button to the messagebox.


Wheb the user click the help button or press F1,the system send aWM_HELP mesasage to the 
owner.
***********************
Page no 22

*MB_OK   :-The Message Contains on push button ok. this is the                      default. 
*MB_OKCANCLE  :- the message box contains two push buttons:ok                                     & cancle
*MB_RETEYCANCLE  :-the message box contains two push                                            buttons:retry & cancle 
*MB_YESNO  :-  the message box contains two push buttons:Yes &                                No 
*MB_YESNOCANCLE  :-the message box contains Three push                                           buttons: yes, no & cancle
*MB_ICONNEXCLAMATION:- An exclamation - point icon appears *MB_ICONWARNING:-          in the message 
*MB_*MB_ICONFORMATION :-   An icon consist of a 
*MB_ICONASTERISK:-   Lower case letter in a circle appears in the 		        message box 
*MB_ICONQUESTION:- A question-mark icon appears in the 		        message box  
*MB_ICONSTOP :-
*MB_ICONERROR:- A stop  - sign icon appears in the 
 *MB_ICONHAND:-  meesage box


If the function succeds, the return value in one of the following menu-item values.
*************************
              value                                             meaning
*IDABORT                                  Abort button was selected
*IDCANCEL                                Cancel button was selected
*IDCONTINUE                           Continue button was selected
*IDIGNORE                                 Ignore button was selected
*IDNO                                          NO button was selected
*IDOK                                          OK button was selected
*IDRETRY                                    Retry button was selected
*IDTRVAGAIN                           Try again button was selected
*IDYES                                          Yes button was selected

If the functions fails,the return zero,To get extended error information,call GetLastError
***********************
pg 24 exp 2	

 REGISTERATING  THE  WINDOWS  CLASS

           A window class has nothing to do a C++ class.Window class window category.Aset of attributes is given a name called as window class name.This class name is later used to create any number of windows.
           RegisterclassEX function links the window class name with the set of attributes of the window. CreateWindowEX function uses the window class name to create a window.

 CREATING  THE  WINDOW

            Create WindowEX function uses the window class name to create a window.
This function allocates memory to the window but does not display it on the screen.

 DISLAYING  THE  WINDOW

           The Show window function displays the window (on the screen) whose handle is specified.It can be shown in different states:restored , maximized , minimized.

 THE  MESSAGE  LOOP

            Messages are represented by numbers and integer constants.
            Message indicates that a paticular event has occurred.
           Eg.:key press , mouse click , maximize close etc.
***************************
Pg no::-25
The message loop continously scans for the message(that are generated  by event like key press or click)
The window is displayed as long as the message loop runs.when the message loop terminates,winmain returns immediately.
Message queue is maintained for each window so that messge 2 can put in it when message 1 is being processed.
Message queue accomphlishes three things:
1] We don't  need to execute two message simultaneously thus avoiding conflicts between them.
2] Message are not missed out.
3]Message are proceed on a first come first server basic.
GetMessage retrives message form message queue.
Translate message does keyboard translation.
Dispatch message passes message to the window as  which then calls the corresponding window procedure.

* The window procedure :-
This the place whrer real action accurs.
This procedure dtermines what is displaed and how inputs are handled.
This procedure is generally not callde directly.
It can have switch case structure. 
***************************
Crate a basic window
#include<window.h>
RESULT CALLBACK WndProc(HWND,UINT,WPARAM,PARAM);
//This is Prototype declaration or forward declaration
//It is the first line of function definition with Semicolon.
//Tells vc++ to look for the function definition anywhere in the program
//without prototype i calling a function before definition will result in "function not defined" compiler errror.
//wndProc function is defined after definition of winMain.
Const char g_SzClassName[]="my window class";
//This is our window class name that will be registered and used in createWindowEx function.
//It is an array of char i.e.a string
 int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR SzCmdLine,intiCmd Show)
{
WNDCLASSEx wc;
//WNDCLASSEx Structure Contain window class information.
HWND hwnd
//variable that will store handle to window
*****************************
MSG msg;
//variable to contain message information
wc.cbsize=sizeof(WNDCLASSEx);
//cbSize should Contain the size of structure itself wc.Style=0;
//Style Specifies the class Style
wc.IpfnWndProc=WndProc;
//WndProc is poniter to wndProc() function wc.cbClsExtra=0;
//Specifies the number of extra butes to allcate following the window_class Structure
wc.cbWndExtra=0;
//Spcifies number of extra butes to allocate following the window instance
wc.hInstance=hInstance;
//Handle to application instance
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
//LoadIcon function gives handle to the icon that the int const IDI_APPLICATION represent
//hIcon member of wc refers to the icon Shown when Alt + Tab is pressed.
//First parameter of LOadIcon=Handle to an instance of the module whose executable file contain the icon
// to be loaded. This parameter must be NULL when a standard icon is being loaded.
wc.hCursor=Load Cursor(NULL,IDE_CROSS);
************************
//first parameter =handle to an instance of the module whoes executable file contains the icon to be loaded.
//this parameter must be NULL when a standard icon is being loaded1
//cursor refers to mouse pointer 
wc.hbrBackground=(HBRUSH)(COLOR-window+1);
//handle to the class background  brush .this member can be handle to the physical brush to be used for.
//painting the background,or it can be a color value1a color value must be one of the following standard
//system colors (the value 1 must be added to the chosen color).if a color value is given,you must convert
//it to HBRUSH type
wc.ipszMenuname=NULL;
//pointer to null terminated character string that specifies the resource name of the class menu specifies the resource name of the class Menu.
wc.ipszClassName=q_szClassName;
//our window class name
wc.hIconsm=load Icon(NULL,IDI application);
//handle the icon that is shown in upper_left corner of application or in the taskbar.
//the register classEx function register a window class for subsequent use in calls to the create windowEx function 
*********************************
messagebox(null "this program requires windows nt","error",MB-ICONATION/MB-OR);
// (handle to owner window,text in messagebox,
message box tiile,message bos style i.e.icon and buttons)
// 1 is a binary or operator.
return 0;
}
hwnd = createwindowExC
ws-Ex-CLIENTEDGE,// entended window style;
specifies that a window has a border with a sunken edge.
9-sz class name           /// reqistered class name
"thye title of my program",// window name.the will be shown ibn the titlebar
WS-OVERLAPPED WNDOW // window style, creates an overlapped window mwith the WS_OVERLAPPED WINDOW
// WS-SYSMENU ,WS-THICKERNAME ,WS-MININMIZEBOX 	and WS-MAXIMIZEBOX styles
CS-USEDEFAULT,   // horizontal position of window CS-USEDEFAULT makes the system select default position
CS-usedefault,  // vertical position of window
240,   //window width
120,  //window height
null // handle to parent or owner window
*************************
NULL                        //menu handle or child identifier
hInstance                  // handle to application instance
NULL                        // window creation data
);
// if the function succeeds ,the return value is handle to the naw window
if(hwnd = NULL)
{
Message Box(NULL, "window creation Failed!" ,"Error!",
MB_ICONEXCLAMETION ! MB_OK);
return(0);
}
Show Window (hwnd ,iCmdShow);           //sets the specified windows show state.
second parameter can be SW_HIDE , SW_RESTORE
UPdate Window (hwnd);      // updates the client area of the specified window by sending a WM_PAINT message to the window.
while (GetMessage (& msg ,NULL,0,0)>0)
//   retrives the message from the calling thread's message queue
//   (message information ,handle to window , first message,last message)
//  handle to window = NULL means it retrives message for any window that belongs to the calling thread
{
TranslateMessage (&msg);    // translates Virtual-key massage into character message.
***********************
//the character messages are posted to the calling threads massage queue,to be read dispatch message(&msq);
//dispatches a message to a window procedure 
}
retuen msg.wparam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM IParam)
//(handle to window, message identifier,first messsage parameter ,second message parameter )
{
switch(msg)
{
case WM_CLOSE:// WM_CLOSE messsage is sent as a signal that window or an appplication should terminate 
// An application can prompt the user for confirmation, prior to destorying a window.
//by processing the WM_CLOSE message and calling the destory window only if the user confirms the choice 
destory window (hwnd);
break;
case WM_DESTORY://The WM_DESTORY message is sent when a window is being destoryed. It is send to the window. 
***********************
//procedure of thwe window being destoryed after the window is removed from the screen.
PostQuit Message(0);
//The post Quitmessage function indicates to the  system that a thread has made a request to terminate .
(quit)
//a specities an application exit code. this value is used as the wparam parameter of the wm_QUIT message
//it causes the get message function to return zero 
break;
default;
return DefwindowProc(hwnd,msg,wParam,lParam);
//The DefwindowProc function calls the default window procedure to provide default processing for any window
//message that an application does not process.
The return value is the result of the message processing 
}
return(0);
}
****************